package com.wen;

import java.math.BigDecimal;

/**
 * @Description:
 * @Author: wenxin
 * @Date: 2022/7/1 9:40
 * @Version: 1.0
 */
public class Solution {


    public int myAtoi(String s) {
        {
            int start = -1, end = -1;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (start == -1 && c != ' ' && c != '_' && c != '+' && c != '-' && !(c >= '0' && c <= '9')) {
                    return 0;
                }
                if (start == -1) {
                    if (c == '+' || c == '-' || (c >= '0' && c <= '9')) {
                        start = i;
                        if (i == s.length() - 1) {
                            end = i + 1;
                        }
                    }
                } else {
                    if (c < '0' || c > '9') {
                        end = i;
                        if (end > start) {
                            break;
                        } else {
                            start = -1;
                            end = -1;
                        }
                    } else if (i == s.length() - 1) {
                        end = i + 1;
                    }
                }
            }
            if (start < end) {
                String numStr = s.substring(start, end);
                if (numStr.equals("+") || numStr.equals("-")) {
                    return 0;
                }
                if (new BigDecimal(numStr).compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) {
                    return Integer.MAX_VALUE;
                } else if (new BigDecimal(numStr).compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) {
                    return Integer.MIN_VALUE;
                }
                return Integer.parseInt(numStr);
            }
            return 0;
        }
    }

    public static void main(String[] args) {


        Solution m=new Solution();
        String s = "  0000000000012345678";
        int t=m.myAtoi(s);
        //boolean t=m.checkMove(board,rMove,cMove,color);
        System.out.println(t);
        //["MyCalendar","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book"]
        //[[],[20,29],[13,22],[44,50],[1,7],[2,10],[14,20],[19,25],[36,42],[45,50],[47,50],[39,45],[44,50],[16,25],[45,50],[45,50],[12,20],[21,29],[11,20],[12,17],[34,40],[10,18],[38,44],[23,32],[38,44],[15,20],[27,33],[34,42],[44,50],[35,40],[24,31]]

    }

}
